package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.CONDICION5;
import ar.edu.caece.compiler.grammar.nonterminals.EXPR;
import ar.edu.caece.compiler.grammar.nonterminals.REL;
import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;

public class CONDICION5Factory extends AbstractTokenVisitorFactory {

	private static CONDICION5Factory INSTANCE;
	
	private EXPRFactory exprFactory;
	private RELFactory relFactory;
	private TokenVisitorInterface parseEXPR;
	private TokenVisitorInterface parseREL;
	
	public static CONDICION5Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new CONDICION5Factory();
		return INSTANCE;
	}
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			exprFactory = EXPRFactory.getInstance();
			relFactory = RELFactory.getInstance();
			parseREL = relFactory.makeVisitor();
			parseEXPR = exprFactory.makeVisitor();
		}
	};
	
	private CONDICION5Factory() throws Exception {

	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador)
					throws Exception {
				GrammarSymbol result = llamador.execute(exprFactory.makeChainedVisitor(siguiente));
				if(result instanceof EXPR) {
					return new CONDICION5 ((EXPR) llamador.execute(parseEXPR), (REL) nextToken().execute(parseREL),(EXPR) nextToken().execute(parseEXPR));
				} 
				else {
					return result;
				}
			}
			
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				return new CONDICION5 ((EXPR) llamador.execute(parseEXPR), (REL) nextToken().execute(parseREL),(EXPR) nextToken().execute(parseEXPR));
			}
		};
	}
	
}
